﻿



$(function () {
    if ($('body').hasClass('loader')) {
        initPreloader();
        preloaderGetImages();
        createPreloading();
        preloaderIETimeout = setTimeout("preloaderIELoadFix()", 10000);
    } else {
        $('body').css('visibility', 'visible');
    }

    $('.logo-pos').css('border', 'none').wrapAll('<a href="/default.aspx" />');

    //    if (window.parent && window.parent != window) {
    //        $('a').attr('target', '_parent');
    //    }

    $('.wait-link').click(function () {
        $(this).text(language.wait);
    });

});



var d = $('<div />');
var l_i = $('<img />');
var ll = $('<div />');
var ll_l = $('<div />');
var lbar = $('<div />');
var cancel_bnt = $('<a />');
var ll_label = $('<div>ŁADOWANIE</div>');
var lperc = $('<div>0%</div>');
var preloader = $("<div></div>");

var preloaderItems = new Array();
var preloaderDoneStatus = 0;
var preloaderDoneNow = 0;
var preloaderIETimeout = 0;

function preloaderIELoadFix() {
    var ie = navigator.userAgent.match(/MSIE (\d+(?:\.\d+)+(?:b\d*)?)/);
    if (ie && ie[0].match("MSIE")) {
        while ((100 / preloaderDoneStatus) * preloaderDoneNow < 100) {
            preloaderImgCallback();
        }
    }
}

function createPreloading() {
    preloader.appendTo('body');
    $(preloader).css({
        height: "0px",
        width: "0px",
        overflow: "hidden"
    });

    var length = preloaderItems.length;
    preloaderDoneStatus = length;

    for (var i = 0; i < length; i++) {
        var imgLoad = $("<img></img>");
        $(imgLoad).attr("src", preloaderItems[i]);
        $(imgLoad).unbind("load");
        $(imgLoad).bind("load", function () {
            preloaderImgCallback();
        });
        $(imgLoad).appendTo($(preloader));
    }
}

function preloaderImgCallback() {
    preloaderDoneNow++;
    preloaderAnimateLoader();
}

function preloaderDoneLoader() {
    clearTimeout(preloaderIETimeout);
    d.fadeOut(1000, function () {
        //    $(this).remove(); 
    });
}

function preloaderAnimateLoader() {

    var perc = Math.ceil((100 / preloaderDoneStatus) * preloaderDoneNow);
    $(lbar).css('width', perc + '%');
    if (perc > 99) {
        preloaderDoneLoader();
    }
    lperc.text(perc + '%');
}

function preloaderGetImages() {
    var everything = $('body').find("*:not(script)").each(function () {
        var url = "";

        if ($(this).css("background-image") != "none" && $(this).css("background-image").indexOf('url') >= 0) {
            var url = $(this).css("background-image");
        } else if (typeof ($(this).attr("src")) != "undefined" && $(this).is('img')) {
            var url = $(this).attr("src");
        }

        url = url.replace("url(\"", "");
        url = url.replace("url(", "");
        url = url.replace("\")", "");
        url = url.replace(")", "");

        if (url.length > 0) {
            preloaderItems.push(url);
        }
    });
}

function initPreloader() {
    $('<img />').attr('src', '/Data/skins/blackhouse/images/load-bcg.jpg');
    $('<img />').attr('src', $('.logo-pos').attr('src'));


    d.css('background', '#000 url(/Data/skins/blackhouse/images/load-bcg.jpg) center no-repeat')
.css('z-index', '1000')
.css('position', 'fixed')
.css('top', '0px')
.css('left', '0px')
.height($(window).height())
.width($(window).width())
.appendTo($('body'));

    l_i.appendTo(d);
    l_i.attr('src', $('.logo-pos').attr('src'));
    l_i.css('position', 'absolute')
.css('left', Math.ceil((d.width() - $('.logo-pos').width()) / 2) + 'px')
.css('top', Math.ceil((d.height() - $('.logo-pos').height()) / 2) + 'px');

    ll.appendTo(d);
    ll.height(40)
.width($('.logo-pos').width())
.css('position', 'absolute')
.css('left', Math.ceil((d.width() - $('.logo-pos').width()) / 2) + 'px')
.css('top', Math.ceil((d.height() - $('.logo-pos').height()) / 2 + $('.logo-pos').height() * 1.2) + 'px');

    ll_l.appendTo(ll);
    ll_l.css('width', '100%')
.css('height', '2px')
.css('position', 'absolute')
.css('top', '19px')
.css('left', '0px')
.css('background-color', '#aaaaaa');

    lbar.appendTo(ll_l);
    lbar.css('width', '0%')
.css('height', '100%')
.css('background-color', '#900');

    ll_label.text(language.loading);
    ll_label.appendTo(ll);
    ll_label.css('font-size', '10px')
.css('color', '#999')
.css('position', 'absolute')
.css('left', '0px')
.css('bottom', '23px');

    lperc.appendTo(ll);
    lperc.css('font-size', '14px')
.css('color', '#999')
.css('position', 'absolute')
.css('right', '0px')
.css('top', '25px');

    cancel_bnt.text(language.skip);
    cancel_bnt.css('position', 'absolute');
    cancel_bnt.css({
        right: Math.ceil(d.width() / 6) + 'px',
        bottom: Math.ceil(d.height() / 6) + 'px'
    });
    cancel_bnt.addClass('cancel-loader');
    cancel_bnt.appendTo(d);
    cancel_bnt.click(function () {
        preloaderDoneLoader();
    });
    $('body').css('visibility', 'visible');
}


$(function () {

    var $ipc = $('<div style="visibility:hidden;position:absolute;bottom:0px;right:0px"></div>');
    $ipc.appendTo($('body'));

    $('.f-social img, .portals img, .change-image img').each(function () {
        $(this).data('src', $(this).attr('src'));

        $('<img />').attr('src', $(this).attr('src').replace('Gray', '')).appendTo($ipc);

    });
    $('.f-social img, .portals img, .change-image img').mouseover(function () {
        $(this).attr('src', $(this).data('src').replace('Gray', ''));
    });
    $('.f-social img, .portals img, .change-image img').mouseout(function () {
        $(this).attr('src', $(this).data('src'));
    });
    $('.f-box-link').mouseover(function () {
        $(this).addClass('f-box-link-a');
    });
    $('.f-box-link').mouseout(function () {
        $(this).removeClass('f-box-link-a');
    });
    $('.res-number').click(function () {
        if ($(this).val().indexOf(' ') >= 0)
            $(this).val('');
    });
});